package com.coderman.datacsdn.service;

import com.coderman.daoclient.bean.KVPair;
import com.coderman.daoclient.dao.IDao;
import com.coderman.daoclient.dao.impl.IDaoImpl;
import com.coderman.datacsdn.model.entity.BlogExt;
import com.coderman.datacsdn.model.entity.CsdnBlog;
import com.coderman.datacsdn.model.entity.CsdnUserEntity;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 58-daojia on 2017-12-23.
 *
 * @author: fanchunshuai
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class ExeInsertDB implements  Runnable {
    private static IDaoImpl<CsdnUserEntity,Long> userDao = new IDaoImpl<>();
    private static IDaoImpl<BlogExt,Long> blogExtLongIDao = new IDaoImpl<>();
    private static IDaoImpl<CsdnBlog,Long>  csdnBlogLongIDao = new IDaoImpl<>();

    private Integer start;
    private Integer end;
    private String  tName;

    public ExeInsertDB(Integer start,Integer end,String tName){
        this.start = start;
        this.end = end;
        this.tName = tName;
    }

    @Override
    public void run() {
        String x=  exeInsert(start,end,tName);
        System.out.println("x = "+x+",tName = "+tName);
    }


    public   String  exeInsert(Integer start,Integer end,String tName){
        String filePath = "E:\\spiderFile\\csdn\\";
        try {
            System.out.println("SELECT id FROM csdn_user LIMIT "+start+","+end);
            List<CsdnUserEntity> list = userDao.getList(CsdnUserEntity.class,"SELECT id FROM csdn_user LIMIT "+start+","+end);
            for (CsdnUserEntity csdnUserEntity : list){
                List<KVPair> kvPairList = new ArrayList<>();
                kvPairList.add(KVPair.build("user_id",csdnUserEntity.getId()));
                List<CsdnBlog> csdnBlogs = csdnBlogLongIDao.getList(CsdnBlog.class, kvPairList);

                for (CsdnBlog csdnBlog : csdnBlogs){
                    String fPath = filePath + csdnBlog.getBlogUrl().replace("/","\\");

                    File file = new File(fPath);
                    if(!file.exists()){
                        file = new File(fPath.replace(".","'."));
                    }

                    if(file.exists()){
                        List<BlogExt> blogExtList = blogExtLongIDao.getList(BlogExt.class,"select id,blog_id from blog_content where blog_id = "+csdnBlog.getId());
                        if(blogExtList !=null && blogExtList.size()>=1){
                            continue;
                        }
                        //String htmlContent = FileUtils.readFileToString(file);
                        Document document = Jsoup.parse(file,"UTF-8");
                        String content = document.text();
                        BlogExt blogExt = new BlogExt();
                        blogExt.setBlogId(csdnBlog.getId());
                        //设置网站来源
                        blogExt.setSiteType("CSDN");
                        //blogExt.setHtmlContent(htmlContent);
                        blogExt.setContent(content);
                        Long ix = blogExtLongIDao.insertEntity(blogExt);
                        if(ix <=0){
                            System.out.println("fPath = "+fPath+",blog_id = "+csdnBlog.getId());
                        }
                        //htmlContent = "";
                        document = null;
                        content = null;

                    }else{
                        System.out.println("wj 不cunz...fPath = "+fPath);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success.......................................success";
    }
}
